package org.liaohailong.library.db.util

import android.text.TextUtils
import org.liaohailong.library.db.annotation.Column
import org.liaohailong.library.db.annotation.Table
import org.liaohailong.library.db.builder.SQLHelper
import org.liaohailong.library.db.model.ColumnInfo
import org.liaohailong.library.db.model.KeyValueInfo
import org.liaohailong.library.db.model.TableInfo

/**
 * Author: liaohailong
 * Date: 2019/4/5
 * Time: 11:44
 * Description: 工具类
 **/
object Utility {

    fun getTableInfo(table: Class<*>): TableInfo {
        var tableName = ""
        var tablePrimaryKey = ""
        val columns = mutableListOf<ColumnInfo>()

        table.getAnnotation(Table::class.java)?.run {
            tableName = if (TextUtils.isEmpty(name)) table.name else name
        }

        for (declaredField in table.declaredFields) {
            for (declaredAnnotation in declaredField.declaredAnnotations) {
                if (declaredAnnotation is Column) {
                    val columnName = if (TextUtils.isEmpty(declaredAnnotation.name)) declaredField.name else declaredAnnotation.name
                    if (declaredAnnotation.primaryKey) {
                        tablePrimaryKey = columnName
                    }
                    val columnType: String = when (declaredField.type) {
                        String::class.java -> SQLHelper.TEXT
                        Int::class.java -> SQLHelper.INTEGER
                        Long::class.java -> SQLHelper.LONG
                        Float::class.java, Double::class.java -> SQLHelper.REAL
                        else -> SQLHelper.TEXT
                    }
                    val columnNotNull = declaredAnnotation.notNull
                    val columnDefault = declaredAnnotation.default
                    val columnInfo = ColumnInfo(columnName, columnType, columnNotNull, columnDefault)
                    columns.add(columnInfo)
                }
            }
        }
        return TableInfo(tableName, tablePrimaryKey, columns)
    }

    fun getWhereParams(item: Any?): MutableMap<String, Any> {
        val where = mutableMapOf<String, Any>()
        item?.apply {
            for (declaredField in javaClass.declaredFields) {
                declaredField.getAnnotation(Column::class.java)?.apply {
                    declaredField.isAccessible = true
                    val value = declaredField.get(item)
                    where[name] = value.toString()
                }
            }
        }
        return where
    }

    /**
     * 根据所有列，找出class中的域集合
     * @param item 挂载数据的bean类
     * @param columns 所有列信息
     */
    fun getFieldListByColumn(item: Any, columns: List<ColumnInfo>): List<KeyValueInfo> {
        val columnFields = mutableListOf<KeyValueInfo>()
        for (column in columns) {
            for (declaredField in item.javaClass.declaredFields) {
                for (declaredAnnotation in declaredField.declaredAnnotations) {
                    if (declaredAnnotation is Column) {
                        if (TextUtils.equals(declaredAnnotation.name, column.name)) {
                            declaredField.isAccessible = true
                            columnFields.add(KeyValueInfo(column.name, declaredField))
                            break
                        }
                    }
                }
            }
        }
        return columnFields
    }
}